﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using DAL;
using Common;

public partial class System_Para : System.Web.UI.Page
{
    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString(); }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        lv_NewsType.DataSource = new Sys_Para().SelectNewsType();
        lv_NewsType.DataBind();

        lv_AccountType.DataSource = new Sys_Para().SelectAccountType();
        lv_AccountType.DataBind();

        lv_MonthDan.DataSource = new Sys_Para().SelectMonthDan();
        lv_MonthDan.DataBind();
    }


    //NewsType
    protected void lv_NewsType_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lv_NewsType.EditIndex = -1;
    }
    protected void lv_NewsType_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        new Sys_Para().DelNewsType(Convert.ToInt32(lv_NewsType.DataKeys[e.ItemIndex].Value));
    }
    protected void lv_NewsType_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lv_NewsType.EditIndex = e.NewEditIndex;
    }
    protected void lv_NewsType_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        NewsType newstype = new NewsType
        {
            TypeKey = Convert.ToInt32(lv_NewsType.DataKeys[e.ItemIndex].Value),
            TypeName = ((TextBox)lv_NewsType.Items[e.ItemIndex].FindControl("txt_TypeName")).Text.Trim()
        };
        new Sys_Para().EditNewsType(newstype);

        lv_NewsType.EditIndex = -1;
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        NewsType newstype = new NewsType
        {
            TypeName = txt_TypeName.Text.Trim()
        };
        new Sys_Para().AddNewsType(newstype);
        txt_TypeName.Text = "";
    }

    //AccountType
    protected void lv_AccountType_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        AccountType accounttype = new AccountType
        {
            TypeKey = Convert.ToInt32(lv_AccountType.DataKeys[e.ItemIndex].Value),
            TypeName = ((TextBox)lv_AccountType.Items[e.ItemIndex].FindControl("txt_TypeName")).Text.Trim()
        };
        new Sys_Para().EditAccountType(accounttype);

        lv_AccountType.EditIndex = -1;

        SystemPara.ActiveTabIndex = 1;
    }
    protected void lv_AccountType_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lv_AccountType.EditIndex = e.NewEditIndex;
        SystemPara.ActiveTabIndex = 1;
    }
    protected void lv_AccountType_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        new Sys_Para().DelAccountType(Convert.ToInt32(lv_AccountType.DataKeys[e.ItemIndex].Value));
        SystemPara.ActiveTabIndex = 1;
    }
    protected void lv_AccountType_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lv_AccountType.EditIndex = -1;
        SystemPara.ActiveTabIndex = 1;
    }
    protected void submit1_Click(object sender, EventArgs e)
    {
        AccountType accounttype = new AccountType
        {
            TypeName = txt_TypeName1.Text.Trim()
        };
        new Sys_Para().AddAccountType(accounttype);
        txt_TypeName1.Text = "";

        SystemPara.ActiveTabIndex = 1;
    }

    //MonthDan
    protected void lv_MonthDan_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int dankey = Convert.ToInt32(((Label)lv_MonthDan.Items[e.ItemIndex].FindControl("DanKey")).Text.Trim());
        string  year = ((DropDownList)lv_MonthDan.Items[e.ItemIndex].FindControl("txt_Year")).SelectedValue;
        string month = ((DropDownList)lv_MonthDan.Items[e.ItemIndex].FindControl("txt_Month")).SelectedValue;
        int monthdans = Convert.ToInt32(((TextBox)lv_MonthDan.Items[e.ItemIndex].FindControl("txt_MonthDan")).Text.Trim());
        int basicnum = Convert.ToInt32(((TextBox)lv_MonthDan.Items[e.ItemIndex].FindControl("txt_BasicNum")).Text.Trim());
        decimal basicmoney = Convert.ToDecimal(((TextBox)lv_MonthDan.Items[e.ItemIndex].FindControl("txt_BasicMoney")).Text.Trim());
        int exceednum = Convert.ToInt32(((TextBox)lv_MonthDan.Items[e.ItemIndex].FindControl("txt_ExceedNum")).Text.Trim());
        decimal exceedmoney = Convert.ToDecimal(((TextBox)lv_MonthDan.Items[e.ItemIndex].FindControl("txt_ExceedMoney")).Text.Trim());

        //Sys_MonthDan old_monthdan = new Sys_Para().SelectMonthDan(dankey);
        //if (new Sys_Para().IsYearMonthExist(year, month) && old_monthdan.Year!=year && old_monthdan.Month!=month)
        //{
        //    WebUtility.JsAlertToBack("已存在相同年月的记录");
        //    Response.End();
        //}

        
        Sys_MonthDan monthdan = new Sys_MonthDan
        {
            DanKey = dankey,
            Year = year,
            Month = month,
            MonthDan = monthdans,
            BasicNum = basicnum,
            BasicMoney = basicmoney,
            ExceedNum = exceednum,
            ExceedMoney = exceedmoney
        };
        new Sys_Para().EditMonthDan(monthdan);

        lv_MonthDan.EditIndex = -1;

        SystemPara.ActiveTabIndex = 2;
    }
    protected void lv_MonthDan_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lv_MonthDan.EditIndex = e.NewEditIndex;
        SystemPara.ActiveTabIndex = 2;
    }
    protected void lv_MonthDan_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        new Sys_Para().DelMonthDan(Convert.ToInt32(lv_MonthDan.DataKeys[e.ItemIndex].Value));
        SystemPara.ActiveTabIndex = 2;
    }
    protected void lv_MonthDan_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lv_MonthDan.EditIndex = -1;
        SystemPara.ActiveTabIndex = 2;
    }
 
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (new Sys_Para().IsYearMonthExist(txt_Year.SelectedValue, txt_Month.SelectedValue))
        {
            WebUtility.JsAlertToBack("已存在相同年月的记录");
            Response.End();
        }

        Sys_MonthDan monthdan = new Sys_MonthDan
        {
            Year = txt_Year.SelectedValue,
            Month = txt_Month.SelectedValue,
            MonthDan = Convert.ToInt32(txt_MonthDan.Text.Trim()),
            BasicNum = Convert.ToInt32(txt_BasicMoney.Text.Trim()),
            BasicMoney = Convert.ToDecimal(txt_BasicMoney.Text.Trim()),
            ExceedNum = Convert.ToInt32(txt_ExceedNum.Text.Trim()),
            ExceedMoney = Convert.ToDecimal(txt_ExceedMoney.Text.Trim())
        };
        new Sys_Para().AddMonthDan(monthdan);

        SystemPara.ActiveTabIndex = 2;
    }

    //
    protected void lv_MonthDan_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            try
            {
                Label L_year = (Label)e.Item.FindControl("L_year");
                Label L_month = (Label)e.Item.FindControl("L_month");
                DropDownList YearList = (DropDownList)e.Item.FindControl("txt_Year");
                DropDownList MonthList = (DropDownList)e.Item.FindControl("txt_Month");
                YearList.SelectedValue = L_year.Text;
                MonthList.SelectedValue = L_month.Text;
            }
            catch (Exception ex)
            {
            }
        }
    }
}
